//
//  ShowBong.m
//  Staff
//
//  Created by HSBC_XiAn_Core on 3/10/15.
//
//

#import "OpenBracelet.h"
#import <bongSDK/bongSDK.h>
#import "AppDelegate.h"
#import "UserInfoManager.h"
#import "APIConnection.h"
#import "ViewController.h"

@implementation OpenBracelet

- (void)openBracelet:(CDVInvokedUrlCommand*)command {
    NSLog(@"command===%@",command);
    if ([command.arguments count]>0) {
        NSString *accountInfo = [[command arguments] objectAtIndex:0];
        NSLog(@"%@",accountInfo);
        
        UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        NSData *inputData = [accountInfo dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        [UserInfoManager defautlUserInfoManager].infoDic = [NSJSONSerialization JSONObjectWithData:inputData options:NSJSONReadingMutableLeaves error:&error];
        
        //bongSDK 配置
        [bongSDK setAppID:@"1424137111684"
                appSecret:@"145538968f85405e8d107ba71847bd85"
                   appKey:@"18052c85a482045cfd2e222759e7093459c5df85"];
        // 设置开发者环境，即服务器为测试环境
        //[bongSDK enableDevelepMode:YES];
        
        NSDictionary *userDic=    [UserInfoManager defautlUserInfoManager].infoDic;
        NSString *account = [userDic objectForKey:@"account"];
        NSString *userName = [userDic objectForKey:@"username"];
        //储存userDefaults
        [[NSUserDefaults standardUserDefaults]setObject:account forKey:USERID];
        [[NSUserDefaults standardUserDefaults]setObject:userName forKey:@"userName"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        //请求网络判断是否登录
        NSDictionary *headerData = @{@"userId":account};
        NSDictionary *data = @{@"userId":account  ,@"userName":userName};
        [[APIConnection shareInstance]post:@"baseInfo/isRegistered" data:data header:headerData netError:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
        } apiFinish:^(id data) {
            
            NSDictionary *dictionaryTemp = data;
            NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
            if (codeTemp ==0) {
                NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
                NSString *strCityId= [dicSuccess objectForKey:@"cityId"];
                BOOL isRegistered = [[dicSuccess objectForKey:@"isRegistered"] boolValue];
                [[NSUserDefaults standardUserDefaults]setObject:@{APIKEYCITYID:strCityId} forKey:APIKEYUSERINFO];
                
                //跳转
                ViewController *contentControl  = [storyboard instantiateViewControllerWithIdentifier:@"viewController"];
                [[(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController].navigationController pushViewController:contentControl animated:YES] ;
                [contentControl setRegisterBool:isRegistered];
            }
            else
            {
            }
        }];
    }
}

@end
